<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Setting and Getting Property Values</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples-basic.html">Basic usage</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="sdo.sample.sequence.html">Working with Sequenced Data Objects</a></div>
 <div class="up"><a href="sdo.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="sdo.sample.getset" class="section">
  <h2 class="title">Setting and Getting Property Values</h2>
  <p class="para"> The following examples assume
   <strong class="command">$company</strong> is the root of a tree of data objects created from the
   schema and instance document shown above.
  </p>
  <p class="para">
   <div class="example" id="example-5324">
    <p><strong>Example #1 Access via property name</strong></p>
    <div class="example-contents"><p>
     Data object properties can be accessed using the object property
     access syntax.  The following sets the company name to &#039;Acme&#039;.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Acme'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5325">
    <p><strong>Example #2 Access via property name as array index</strong></p>
    <div class="example-contents"><p>We can also access properties using associative array syntax. The simplest
     form of this uses the property name as the array index. For example, the following sets
     the company name and gets the employeeOfTheMonth.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'UltraCorp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$eotm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'employeeOfTheMonth'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5326">
    <p><strong>Example #3 Data Object iteration</strong></p>
    <div class="example-contents"><p>
     We can iterate over the properties of a data object using <a href="control-structures.foreach.html" class="link">foreach</a>.
     The following iterates over the properties of the employee of the month.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$eotm&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">;<br />&nbsp;&nbsp;foreach&nbsp;(</span><span style="color: #0000BB">$eotm&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$name</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p> 
     which will output:
    </p></div>     
    <div class="example-contents">
<div class="cdata"><pre>
name: Jane Doe
SN: E0003
</pre></div>
    </div>

    <div class="example-contents"><p> 
     The &#039;manager&#039; property is not output, because it has not been set.
    </p></div>
   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-5327">
    <p><strong>Example #4 Access many-valued property by name</strong></p>
    <div class="example-contents"><p> Many-valued data object properties can also be accessed using the object
     property name syntax. The following gets the list of departments.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$departments&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-5328">
    <p><strong>Example #5 Many-valued element access</strong></p>
    <div class="example-contents"><p>
     We can access individual elements of many-valued properties using array
     syntax. The following accesses the first department in the company.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$ad_tech_dept&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5329">
    <p><strong>Example #6 Many-valued property iteration</strong></p>
    <div class="example-contents"><p>
     Many-valued properties can also be iterated over using
     <a href="control-structures.foreach.html" class="link">foreach</a>.  The following iterates over the company&#039;s departments.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;</span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$department</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;...<br />&nbsp;&nbsp;</span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Each iteration will assign the next department in the
     list to the variable <strong class="command">$department</strong>.
    </p></div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5330">
    <p><strong>Example #7 Chained property access</strong></p>
    <div class="example-contents"><p> We can chain property references on a single line. 
     The following sets and gets the name of the first department.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Emerging&nbsp;Technologies'</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$dept_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>Using the associative array syntax, this is equivalent to</p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'departments'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'name'</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'Emerging&nbsp;Technologies'</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$dept_name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">'departments'</span><span style="color: #007700">][</span><span style="color: #0000BB">0</span><span style="color: #007700">][</span><span style="color: #DD0000">'name'</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p> In either case, the dept_name variable is set to &#039;Emerging Technologies&#039;.
    </p></div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5331">
    <p><strong>Example #8 XPath navigation</strong></p>    
    <div class="example-contents"><p> The associative array index can be an XPath-like expression. Valid
     expressions are defined by an augmented sub-set of XPath.
    </p></div>
    <div class="example-contents"><p>
     Two forms of indexing into many-valued properties are supported.  
     The first is the standard XPath array syntax with the indexing
     starting at one, the second is an SDO extension to XPath with an index
     starting at zero.  The standard syntax is:
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$jane_doe&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[1]/employees[2]"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>
    
    <div class="example-contents"><p>and the SDO XPath extension syntax is:</p></div> 
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$jane_doe&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments.0/employees.1"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>
     Both these examples get the second employee from the first department.
    </p></div>
   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5332">
    <p><strong>Example #9 XPath querying</strong></p>
    <div class="example-contents"><p>
     We can use XPath to query and identify parts of a data object based
     on instance data.  The following retrieves the manager from the
     &#039;Advanced Technologies&#039; department.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;$ad_tech_mgr&nbsp;</span><span style="color: #007700">=&nbsp;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[name='Advanced&nbsp;Technologies']/employees[manager=true]"</span><span style="color: #007700">];<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5333">
    <p><strong>Example #10 Creating child data objects</strong></p>
    <div class="example-contents"><p>
     A data object can be a factory for its child data objects.  
     A child data object is automatically part of the data graph.  
     The following add a new employee to the &#039;Advanced Technologies&#039; 
     department.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$ad_tech_dept&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$company</span><span style="color: #007700">[</span><span style="color: #DD0000">"departments[name='Advanced&nbsp;Technologies']"</span><span style="color: #007700">];<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$new_hire&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$ad_tech_dept</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createDataObject</span><span style="color: #007700">(</span><span style="color: #DD0000">'employees'</span><span style="color: #007700">);<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'John&nbsp;Johnson'</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">SN&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'E0005'</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$new_hire</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">manager&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>

  <p class="para">
   <div class="example" id="example-5334">
    <p><strong>Example #11 Unset a primitive property</strong></p>
    <div class="example-contents"><p>
     We can use the <span class="function"><a href="function.isset.html" class="function">isset()</a></span> and
     <span class="function"><a href="function.unset.html" class="function">unset()</a></span> functions to test and remove items
     from the data object.
    </p></div>
    <div class="example-contents"><p>
     The following clears the name of the first department.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;</span><span style="color: #007700">unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
  </p>
     
  <p class="para">
   <div class="example" id="example-5335">
    <p><strong>Example #12 Unset a data object</strong></p>
    <div class="example-contents"><p>
     unset can also be used to remove a data object from the tree.
     The following example shows John Jones leaving the company. 
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;</span><span style="color: #007700">unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">departments</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]-&gt;</span><span style="color: #0000BB">employees</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-5336">
    <p><strong>Example #13 Unset a referenced data object</strong></p>
    <div class="example-contents"><p>
     The following removes the &#039;employeeOfTheMonth&#039; from the company.
     If this were a containment relationship then the
     employee would be removed from the company 
     (probably not a good idea to sack your best employee each month!), 
     but since this is a non-containment reference, 
     the employee being referenced will remain in the 
     department in the company,
     but will no longer be accessible via the employeeOfTheMonth property.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(isset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;unset(</span><span style="color: #0000BB">$company</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">employeeOfTheMonth</span><span style="color: #007700">);<br />&nbsp;&nbsp;}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  
  <p class="para">
   <div class="example" id="example-5337">
    <p><strong>Example #14 Access via property index</strong></p>
    <div class="example-contents"><p> Data object properties can be accessed via their property index using array
     syntax. The property index is the position at which the property&#039;s definition
     appears in the model (in this case the xml schema). We can see from the schema listing
     above that the company name attribute is the second company property (the SDO
     interface makes no distinction between XML attributes and elements). The following
     sets the company name to &#039;Acme&#039;, with the same result as
     <a href="sdo.sample.getset.html#sdo.examples.propname" class="link">Access via property name</a>
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;$company</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #DD0000">'Acme'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p> Using the index directly in this way is likely to be fragile. Normally the
     property name syntax should be preferred, but the property index may be required
    in special cases.
    </p></div>
   </div>
  </p>

 </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="sdo.examples-basic.html">Basic usage</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="sdo.sample.sequence.html">Working with Sequenced Data Objects</a></div>
 <div class="up"><a href="sdo.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
